package  main.java.leetcode.editor.cn;
//2023-07-04 16:02:16
//实现一个函数，检查二叉树是否平衡。在这个问题中，平衡树的定义如下：任意一个节点，其两棵子树的高度差不超过 1。 示例 1: 给定二叉树 [3,9,20,
//null,null,15,7]     3    / \   9  20     /  \    15   7 返回 true 。示例 2: 给定二叉树 [1,2,
//2,3,3,null,null,4,4]       1      / \     2   2    / \   3   3  / \ 4   4 返回 
//false 。 Related Topics 树 深度优先搜索 二叉树 👍 108 👎 0

class CheckBalanceLcci {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new CheckBalanceLcci().new Solution();
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        return root == null ? true : Math.abs(getHigh(root.right)-getHigh(root.left)) <= 1
                && isBalanced(root.left) && isBalanced(root.right);
    }

    private int getHigh(TreeNode treeNode){
        return treeNode == null ? 0 : Math.max(getHigh(treeNode.left),getHigh(treeNode.right))+1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
